//
//  SDServerSwitchManager.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDServerSwitchManager.h"

const int SERVER_RELEASE = 0;
const int SERVER_PRERELEASE = 1;
const int SERVER_DEBUG = 2;

@interface ServerInfo()
@property(nonatomic,strong,readwrite)NSString* serverAddress;
@property(nonatomic,strong,readwrite)NSString* port;
@end

@implementation ServerInfo

@end

@implementation SDServerSwitchManager
+(SDServerSwitchManager*)sharedInstance
{
    static SDServerSwitchManager* sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[SDServerSwitchManager alloc] init];
    });
    
    return sharedInstance;
}

-(id)init
{
    self = [super init];
    if (self) {
        _curServerType = SERVER_TYPE;
#ifndef kBuildAppForAppStore
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSNumber *typeObj = [userDefaults objectForKey:@"kSERVERTYPE"];
        if (typeObj)
        {
            _curServerType = typeObj.intValue;
        }
        else
        {
            [userDefaults setObject:[NSNumber numberWithInt:_curServerType] forKey:@"kSERVERTYPE"];
            [userDefaults synchronize];
        }
#endif
    }
    
    return self;
}

-(void)switchServer:(int)serverType
{
    if (_curServerType == serverType)
        return;
    
    _curServerType = serverType;
    
  //  IP_Address = nil;
}

-(NSString*)getServerUrl
{
    ServerInfo* serverInfo = [[SDServerSwitchManager sharedInstance] getServerInfo];
    return serverInfo.serverAddress;
}

-(ServerInfo*)getServerInfo
{
    ServerInfo* curSeverInfo = nil;
    switch (_curServerType) {
        case SERVER_RELEASE:
            curSeverInfo = [self releaseSeverInfo];
            break;
        case SERVER_PRERELEASE:
            curSeverInfo = [self preReleaseSeverInfo];
            break;
        case SERVER_DEBUG:
            curSeverInfo = [self debugSeverInfo];
            break;
        default:
            curSeverInfo = [self releaseSeverInfo];
            break;
    }
    
    return curSeverInfo;
}

#pragma mark-
#pragma mark Private Methods
-(ServerInfo*)debugSeverInfo
{
    ServerInfo* debugServInfo = [[ServerInfo alloc] init];
    debugServInfo.serverAddress = @"http://121.41.53.41:8877";
    debugServInfo.port = @"8877";
    
    return debugServInfo;
}

-(ServerInfo*)releaseSeverInfo
{
    ServerInfo* releaseServInfo = [[ServerInfo alloc] init];
    releaseServInfo.serverAddress = @"http://appservice.jifurizhi.com:8877";
    releaseServInfo.port = @"8877";
    
    return releaseServInfo;
}

-(ServerInfo*)preReleaseSeverInfo
{
    ServerInfo* preReleaseServInfo = [[ServerInfo alloc] init];
    preReleaseServInfo.serverAddress = @"http://121.41.53.41:8877";
    preReleaseServInfo.port = @"8877";
    
    return preReleaseServInfo;
}
@end

